package com.aidex.common.utils.sms;
import com.aidex.common.utils.StringUtils;
import com.alibaba.fastjson.JSONObject;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.Map;

public class ChuangLanSmsClient {

    private static final String account = "N819765_N7404742";
    private static final String password = "jVBGRgDo0uab94";
    private static final String sendUrl = "https://smssh1.253.com/msg/variable/json";
    public static String sendSmsByPost(String path, String postContent) {
        URL url = null;
        try {
            url = new URL(path);
            HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
            httpURLConnection.setRequestMethod("POST");
            httpURLConnection.setConnectTimeout(10000);
            httpURLConnection.setReadTimeout(10000);
            httpURLConnection.setDoOutput(true);
            httpURLConnection.setDoInput(true);
            httpURLConnection.setRequestProperty("Charset", "UTF-8");
            httpURLConnection.setRequestProperty("Content-Type", "application/json");
            httpURLConnection.connect();
            OutputStream os=httpURLConnection.getOutputStream();
            if(StringUtils.isNotEmpty(postContent)){
                os.write(postContent.getBytes("UTF-8"));
            }

            os.flush();
            StringBuilder sb = new StringBuilder();
            int httpRspCode = httpURLConnection.getResponseCode();
            if (httpRspCode == HttpURLConnection.HTTP_OK) {
                BufferedReader br = new BufferedReader(
                        new InputStreamReader(httpURLConnection.getInputStream(), "utf-8"));
                String line = null;
                while ((line = br.readLine()) != null) {
                    sb.append(line);
                }
                br.close();
                return sb.toString();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }


    public static JSONObject sendSms(String params){

        Map map = new HashMap();
        map.put("account",account);//API账号
        map.put("password",password);//API密码
        map.put("msg","【车来钱】温馨提示：您有新客户： {$var} ，请及时登录系统进行跟进。");//短信内容
        map.put("params",params);//手机号
        map.put("report","true");//是否需要状态报告
        map.put("extend","356079");//自定义扩展码
        JSONObject js = (JSONObject) JSONObject.toJSON(map);
        return JSONObject.parseObject(sendSmsByPost(sendUrl,js.toString()));

    }

    public static JSONObject sendVariableSms(String params){

        Map map = new HashMap();
        map.put("account",account);//API账号
        map.put("password",password);//API密码
        map.put("msg","【车来钱】尊敬的{$var}，您好！您的预约已登记成功，请您于{$var}前上门办理业务，预约号{$var}，我们将竭诚为您服务。");//短信内容
        map.put("params",params);//手机号
        map.put("report","true");//是否需要状态报告
        map.put("extend","001");//自定义扩展码
        JSONObject js = (JSONObject) JSONObject.toJSON(map);
        return JSONObject.parseObject(sendSmsByPost(sendUrl,js.toString()));

    }

    public static void main(String[] args) throws ParseException {


        JSONObject jsonObject = sendSms("17771704470,熊先生");
        System.out.println(jsonObject);

    }
}
